﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;
using System.IO;
using System.Web.Security;

namespace MAT.Controllers
{
    class ctrLogin
    {
        //public static frmMain fMain = null;
        public static frmLogin _frmLogin = null;
        Controllers.ctrNotice _ctrNotice = new Controllers.ctrNotice();
        public frmLogin FrmLogin
        {
            get {return _frmLogin;}
            set {_frmLogin = value;}
        }

        MAT_Context context = MATContext.getContext();
        int count = 3;
        public static int priority = 0;
        public static string Username = "";
        public static string employee_ID = "";

        // Check textbox Username, Password
        private bool check(TextBox us, TextBox pw)
        {
            if (us.Text == "")
            {
                MessageBox.Show("You don't enter Username");
                us.Focus();
                return false;
            }
            else if (pw.Text == "")
            {
                MessageBox.Show("You don't enter Password");
                pw.Focus();
                return false;
            }
            return true;

        }
        // Display message welcome user.
        private void welcome(string k)
        {
            MessageBox.Show("Welcome " + k.ToUpper() + " log into MAT System");
        }

        public void frmLogin_Load()
        {
            _frmLogin.txtUsername.Text = "";
            _frmLogin.txtPassword.Text = "";
            _frmLogin.txtUsername.Focus();
        }

        public void btnCancel_Click()
        {
            //Delete file log
            if (!File.Exists(Application.StartupPath + @"\Log.txt"))
                File.Create(Application.StartupPath + @"\Log.txt");
            StreamWriter ghi = new StreamWriter(Application.StartupPath + @"\Log.txt");
            ghi.Flush();
            ghi.Write("");
            ghi.Close();
            Application.Exit();
        }

        public void frmLogin_FormClosed()
        {
            Application.Exit();
        }

        public void btnSignIn_Click()
        {
            // Check (check) method
            if (check(_frmLogin.txtUsername, _frmLogin.txtPassword))
            {
                string useName = _frmLogin.txtUsername.Text;
                string password = FormsAuthentication.HashPasswordForStoringInConfigFile(_frmLogin.txtPassword.Text, "MD5");
                LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.USERNAME == useName 
                                                            && log.PASSWORDS == password);
                if (login != null)
                {
                    if (login.LOGGED == 0)
                    {
                        //Set flag for user (logged in)
                        login.LOGGED = 1;
                        context.SaveChanges();

                        priority = Convert.ToInt16(login.PRIORITY);
                        Username = _frmLogin.txtUsername.Text.ToUpper();
                        employee_ID = login.EMPLOYEE_ID;
                        welcome(_frmLogin.txtUsername.Text);
                        _frmLogin.Visible = false;
                        MAT.Controllers.ctrOption._frmOption = new frmOption();
                        MAT.Controllers.ctrOption._frmOption.Show();
                    }
                    else
                    {
                        MessageBox.Show("Duplicate login information!");
                        _frmLogin.txtUsername.Text = "";
                        _frmLogin.txtPassword.Text = "";
                        _frmLogin.txtUsername.Focus();
                    }
                }
                else
                {
                    MessageBox.Show(_frmLogin, "User account is not available, please enter again. \n You have " + --count + " times!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _frmLogin.txtUsername.Text = "";
                    _frmLogin.txtPassword.Text = "";
                    _frmLogin.txtUsername.Focus();
                }
                if (count == 0)
                {
                    MessageBox.Show("You enter over 3 times");
                    Application.Exit();
                }
            }
        }

        public static void writeFile(string un, int prio, string empID)
        {
            if (!File.Exists(Application.StartupPath + @"\Log.txt"))
                File.Create(Application.StartupPath + @"\Log.txt");
            StreamWriter ghi = new StreamWriter(Application.StartupPath + @"\Log.txt");
            ghi.Flush();
            ghi.Write(un + "|" + prio + "|" + empID);
            ghi.Close();
        }

        public void keyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSignIn_Click();
            }
        }
    }
}
